// PROGRAMA : SCRP140A
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : SSP - SECRETARIA DE SEGURAN�A PUBLICA/INSTITUTO
// OBJETIVO : CONSULTAS
// PROGRAMADOR: JOSE ANTONIO COELHO
// DATA : 11 DE SETEMBRO DE 1997
// ALTERACAO : PERMITIR CONSULTAS DAS FICHAS CRIMINAIS USANDO
// OS CAMPOS FONETIZADOS(NOME, NOME DA MAE, DO PAI
// E ALCUNHAS).
// ALTERACAO : 04/11/1999 TELA E DATAS (N6) PARA (N8) POR SOLON
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.util.Arrays;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrm1410;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp140aA;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp140aB;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp140aC;
import br.com.vti.sisp_sicrim.antigo.model.SicCadastro;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrp140a OBJETIVO : CONSULTAS *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrp140a")
public class Scrp140a {
	// Views utilizadas
	SicCadastro criminal = new SicCadastro();
	// Telas/relatorios utilizados
	Scrm1410 scrm1410;
	Scrp140aA scrp140aA;
	Scrp140aB scrp140aB;
	Scrp140aC scrp140aC;
	// Includes utilizados
	// Areas globais / session
	Scrg001 scrg001;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrp140a scrp140aAnterior;
	String faseproc = "0";

	// private String[] jOpc = new String[5];
	private int jDataIni = 0;
	private int jDataFim = 0;
	private int jData = UtilBean.obtemDataCorrente();
	private String mensagemInicial = "";

	private Scrm1410 preMapas() {
		sessao.getScrg001().setWFuncaow("CONSULTAS");
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());

		scrm1410 = new Scrm1410(true, sessao);
		scrp140aA = new Scrp140aA(true, sessao);
		scrp140aB = new Scrp140aB(true, sessao);
		scrp140aC = new Scrp140aC(true, sessao);

		scrm1410.setDataCorrente(UtilBean.obtemDataDia());
		Arrays.fill(scrm1410.getJopc(), "");
		scrm1410.setJopc(scrm1410.getJopc());
		return scrm1410;
	}

	public Scrp140a(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Scrm1410 scrm1410, Scrp140aA scrp140aA, Scrp140aB scrp140aB, Scrp140aC scrp140aC) {
		try {
			main(scrm1410, scrp140aA, scrp140aB, scrp140aC);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Scrm1410 pscrm1410, Scrp140aA pscrp140aA, Scrp140aB pscrp140aB, Scrp140aC pscrp140aC) {
		if (pscrm1410 == null && pscrp140aA == null && pscrp140aB == null && pscrp140aC == null) {
			primeiraVez = true;
			// scrm1410 = new Scrm1410 (primeiraVez);
			scrm1410 = preMapas();
			scrm1410.setNavegar("../scrp140a/executar");
			scrm1410.setMensagem(mensagemInicial);
			result.include("scrm1410", scrm1410);
			sessao.setAtributo("SCRP140A", this);
			scrm1410.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pscrm1410, pscrp140aA, pscrp140aB, pscrp140aC);
		}

		scrm1410.receberTela(false, result);
		if (!scrm1410.getJopc(1).trim().equals("")) {
			Scrp142a scrp142a = new Scrp142a(result, sessao);
			scrp142a.executar(null);
			return;
		} else {
			if (!scrm1410.getJopc(2).trim().equals("")) {
				if (verificaData() == 1) {
					Scrp141a scrp141a = new Scrp141a(result, sessao);
					scrp141a.jDataIni = scrp140aB.getJdataini();
					scrp141a.executar(null);
					return;
				}
				return;
			} else {
				if (!scrm1410.getJopc(3).trim().equals("")) {
					if (verificaData() == 1) {
						Scrp1430 scrp1430 = new Scrp1430(result, sessao);
						scrp1430.executar(null, scrp140aB.getJdataini());
					}
					return;
				} else {
					if (!scrm1410.getJopc(4).trim().equals("")) {
						if (!scrp140aA.getEnviada()) {
							result.include("scrp140aA", scrp140aA);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							sessao.setAtributo("SCRP140A", this);
							return;
						}
						if (pscrp140aA != null) {
							scrp140aA.receberTela(false, result);
						}
						if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
							scrp140aA.enviarMensagem(null, "TECLA INVALIDA");
							return;
						}
						if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
							Scrp140a scrp140a = new Scrp140a(result, sessao);
							scrp140a.executar(null, null, null, null);
							return;
						}
						if (Integer.parseInt(scrp140aA.getJdiaini()) == 0 || Integer.parseInt(scrp140aA.getJmesini()) == 0 || Integer.parseInt(scrp140aA.getJanoini()) == 0) {
							scrp140aA.enviarMensagem("jdiaini", "DATA INICIAL INVALIDA");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (Integer.parseInt(scrp140aA.getJanoini()) < 1997) {
							scrp140aA.enviarMensagem("janoini)", "ANO INVALIDO");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (scrp140aA.getjDataIni() > jData) {
							scrp140aA.enviarMensagem("jdiaini", "DATA INVALIDA");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (Integer.parseInt(scrp140aA.getJdiafim()) == 0 || Integer.parseInt(scrp140aA.getJmesfim()) == 0 || Integer.parseInt(scrp140aA.getJanofim()) == 0) {
							scrp140aA.enviarMensagem("jdiafim", "DATA FINAL INVALIDA");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (Integer.parseInt(scrp140aA.getJanofim()) < 1997) {
							scrp140aA.enviarMensagem("janofim", "ANO INVALIDO");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (scrp140aA.getjDataFim() < scrp140aA.getjDataIni()) {
							scrp140aA.enviarMensagem("jdiafim", "DATA FINAL INVALIDA");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						if (scrp140aA.getjDataFim() > jData) {
							scrp140aA.enviarMensagem("jdiafim", "DATA FINAL INVALIDA");
							result.include("scrp140aA", scrp140aA);
							sessao.setAtributo("SCRP140A", this);
							scrp140aA.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
							return;
						}
						Scrp1440 scrp1440 = new Scrp1440(result, sessao);
						scrp1440.executar(null, null, scrp140aA.getjDataIni(), scrp140aA.getjDataFim());
						return;
					}
				}
			}
		}
		if (!UtilBean.validaSelecao(scrm1410.getJopc())) {
			Scrp1000 scrp1000 = new Scrp1000(result, sessao);
			scrp1000.executar(null);
			return;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp1000 scrp1000 = new Scrp1000(result, sessao);
			scrp1000.executar(null);
			return;
		}
		sessao.setAtributo("SCRP140A", this);
		result.include("scrm1410", scrm1410);
		scrm1410.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private int verificaData() {
		if (!scrp140aB.getEnviada()) {
			result.include("scrp140aB", scrp140aB);
			scrp140aB.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("SCRP140A", this);
			return 0;
		}
		scrp140aB.receberTela(false, result);
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp140aB.enviarMensagem(null, "TECLA INVALIDA");
			sessao.setAtributo("SCRP140A", this);
			result.include("scrp140aB", scrp140aB);
			scrp140aB.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp140a scrp140a = new Scrp140a(result, sessao);
			scrp140a.executar(null, null, null, null);
			return 0;
		}
		if (scrp140aB.getJdiaini() == 0 || scrp140aB.getJmesini() == 0 || scrp140aB.getJanoini() == 0) {
			scrp140aB.enviarMensagem("jdiaini", "Data Inicial invalida");
			sessao.setAtributo("SCRP140A", this);
			result.include("scrp140aB", scrp140aB);
			scrp140aB.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		if (scrp140aB.getJanoini() < 1997) {
			scrp140aB.enviarMensagem("janoini", "Ano invalido");
			sessao.setAtributo("SCRP140A", this);
			result.include("scrp140aB", scrp140aB);
			scrp140aB.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		if (scrp140aB.getJdataini() > jData) {
			scrp140aB.enviarMensagem("jdiaini", "Data invalida");
			sessao.setAtributo("SCRP140A", this);
			result.include("scrp140aB", scrp140aB);
			scrp140aB.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		// Par�grafo: fd1()
		if (criminal.consultaDataDocAtuStatusRegDifer(scrp140aB.getJdataini(), 5)) {
			scrp140aC.setJreg(criminal.totalRegistros());
		}
		criminal.fecha();
		//
		if (!scrp140aC.getEnviada()) {
			scrp140aB.enviarMensagem("jdiaini", "Foram digitadas \"" + scrp140aC.getJreg() + "\" fichas nesse per�odo.");
			result.include("scrp140aB", scrp140aB);
			sessao.setAtributo("SCRP140A", this);
			scrp140aB.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		scrp140aC.receberTela(false, result);
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp140aB.enviarMensagem(null, "Tecla invalida");
			sessao.setAtributo("SCRP140A", this);
			result.include("scrp140aC", scrp140aC);
			scrp140aC.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return 0;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp140a scrp140a = new Scrp140a(result, sessao);
			scrp140a.executar(null, null, null, null);
			return 0;
		}
		return 1;
	}

	private void restaurarEstados(Scrm1410 pscrm1410, Scrp140aA pscrp140aA, Scrp140aB pscrp140aB, Scrp140aC pscrp140aC) {
		this.scrp140aAnterior = (Scrp140a) sessao.getAtributo("SCRP140A");
		if (pscrm1410 != null) {
			scrm1410 = pscrm1410;
			scrm1410.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrm1410.setJprogramaj(sessao.getScrg001().getWProgramaw());
			scrm1410.setJversaoj(sessao.getScrg001().getWVersaow());
			scrm1410.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrm1410 = this.scrp140aAnterior.scrm1410;
		}
		scrm1410.setSessao(sessao);
		if (pscrp140aA != null) {
			scrp140aA = pscrp140aA;
			scrp140aA.setjempresaj(sessao.getScrg001().getWEmpresaw());
			scrp140aA.setjprogramaj(sessao.getScrg001().getWProgramaw());
			scrp140aA.setjversaoj(sessao.getScrg001().getWVersaow());
			scrp140aA.setjfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp140aA = this.scrp140aAnterior.scrp140aA;
		}
		scrp140aA.setSessao(sessao);
		if (pscrp140aB != null) {
			scrp140aB = pscrp140aB;
			scrp140aB.setjempresaj(sessao.getScrg001().getWEmpresaw());
			scrp140aB.setjprogramaj(sessao.getScrg001().getWProgramaw());
			scrp140aB.setjversaoj(sessao.getScrg001().getWVersaow());
			scrp140aB.setjfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp140aB = this.scrp140aAnterior.scrp140aB;
		}
		scrp140aB.setSessao(sessao);
		if (pscrp140aC != null) {
			scrp140aC = pscrp140aC;
			scrp140aC.setjempresaj(sessao.getScrg001().getWEmpresaw());
			scrp140aC.setjprogramaj(sessao.getScrg001().getWProgramaw());
			scrp140aC.setjversaoj(sessao.getScrg001().getWVersaow());
			scrp140aC.setjfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp140aC = this.scrp140aAnterior.scrp140aC;
		}
		scrp140aC.setSessao(sessao);
		this.faseproc = this.scrp140aAnterior.faseproc;
		this.scrp140aA.setEnviada(this.scrp140aAnterior.scrp140aA.getEnviada());
		this.scrp140aB.setEnviada(this.scrp140aAnterior.scrp140aB.getEnviada());
		this.scrp140aC.setEnviada(this.scrp140aAnterior.scrp140aC.getEnviada());
	}

	protected void setMensagem(String mensagem) {
		mensagemInicial = mensagem;
	}
}
